package com.jp.knowledge.my.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.my.adapter.ShareInviteAdapter;
import com.jp.knowledge.my.base.BasePopWindow;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.OptionSelectModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author gzejia 978862664@qq.com
 */
public class ShareInvitePw extends BasePopWindow {

    public static final int POPWIN_TYPE_SHARE = 0;
    public static final int POPWIN_TYPE_INVITE = 1;

    /**
     * @param context Context
     * @param type    {@link #POPWIN_TYPE_INVITE} 邀请；{@link #POPWIN_TYPE_SHARE} 分享
     */
    public ShareInvitePw(Context context, int type) {
        super(context);
        initPopView(type);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_share_invite;
    }

    private void initPopView(int type) {
        ShareInviteAdapter quoteAdapter = new ShareInviteAdapter(
                mContext, getOptionData(type == POPWIN_TYPE_SHARE ? 1 : 3));
        quoteAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                dismiss();
                if (null == mListener) return;
                mListener.quoteClick(position);
            }
        });
        RecyclerView mQuoteRv = (RecyclerView) mView.findViewById(R.id.dialog_rv_share_quote);
        mQuoteRv.setHasFixedSize(true);
        mQuoteRv.setLayoutManager(new LinearLayoutManager(
                mContext, LinearLayoutManager.HORIZONTAL, false));
        mQuoteRv.setAdapter(quoteAdapter);

        ShareInviteAdapter systemAdapter = new ShareInviteAdapter(
                mContext, getOptionData(type == POPWIN_TYPE_SHARE ? 2 : 4));
        systemAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                dismiss();
                if (null == mListener) return;
                mListener.systemClick(position);
            }
        });
        RecyclerView mSystemRv = (RecyclerView) mView.findViewById(R.id.dialog_rv_share_system);
        mSystemRv.setHasFixedSize(true);
        mSystemRv.setLayoutManager(new LinearLayoutManager(
                mContext, LinearLayoutManager.HORIZONTAL, false));
        mSystemRv.setAdapter(systemAdapter);

        mView.findViewById(R.id.dialog_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    /**
     * @param type 0_分享顶部内容，1_分享底部内容，2_邀请顶部内容，3_邀请底部内容
     * @return 选项集合
     */
    private List<OptionSelectModel> getOptionData(int type) {
        String[] contentShare1 = {"微信朋友圈", "微信好友", "手机QQ", "QQ空间", "新浪微博", "腾讯博客"};
        Integer[] imgShare1 = {R.mipmap.ic_my_header, R.mipmap.ic_my_header, R.mipmap.ic_my_header,
                R.mipmap.ic_my_header, R.mipmap.ic_my_header, R.mipmap.ic_my_header};

        String[] contentShare2 = {"复制链接", "短信"};
        Integer[] imgShare2 = {R.mipmap.ic_my_header, R.mipmap.ic_my_header};

        String[] contentInvite3 = {"手机联系人", "微信好友", "手机QQ", "二维码邀请"};
        Integer[] imgInvite3 = {R.mipmap.ic_my_header, R.mipmap.ic_my_header, R.mipmap.ic_my_header,
                R.mipmap.ic_my_header};

        String[] contentInvite4 = {"电脑端导入", "复制链接", "短信"};
        Integer[] imgInvite4 = {R.mipmap.ic_my_header, R.mipmap.ic_my_header, R.mipmap.ic_my_header};

        List<String> content = Arrays.asList(type == 1 ? contentShare1 : type == 2 ? contentShare2 :
                type == 3 ? contentInvite3 : contentInvite4);
        List<Integer> img = Arrays.asList(type == 1 ? imgShare1 : type == 2 ? imgShare2 :
                type == 3 ? imgInvite3 : imgInvite4);
        List<OptionSelectModel> models = new ArrayList<>();

        for (int i = 0, k = content.size(); i < k; i++) {
            OptionSelectModel model = new OptionSelectModel();
            model.mContent = content.get(i);
            model.mImage = img.get(i);
            models.add(model);
        }

        return models;
    }

    public void addOnOptionSelectListener(OnOptionSelectListener listener) {
        mListener = listener;
    }

    private OnOptionSelectListener mListener;

    /**
     * 监听选项需自行对应
     */
    public interface OnOptionSelectListener {
        void quoteClick(int position);

        void systemClick(int position);
    }
}
